package com.hk.commons.jackson;

import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.hk.commons.jackson.annotations.DictField;
import com.hk.commons.jackson.annotations.EnumField;
import com.hk.commons.jackson.annotations.SensitiveField;
import com.hk.commons.jackson.annotations.XssField;

import java.util.Objects;

/**
 * 自定义注解解析json序列化与反序列化数据
 *
 * @author Kevin
 * @date 2024-11-21 11:55
 */
public class EnhanceAnnotationIntrospector extends JacksonAnnotationIntrospector {

    /**
     * 查询序列化器
     */
    @Override
    public Object findSerializer(Annotated annotated) {
        SensitiveField sensitiveField = annotated.getAnnotation(SensitiveField.class);
        if (Objects.nonNull(sensitiveField)) {
            return new SensitiveSerializer(sensitiveField);
        }
        DictField dictField = annotated.getAnnotation(DictField.class);
        if (Objects.nonNull(dictField)) {
            return new DictSerializer(dictField);
        }
        XssField xssField = annotated.getAnnotation(XssField.class);
        if (Objects.nonNull(xssField)) {
            return new XssStringJsonSerializer();
        }
        EnumField enumField = annotated.getAnnotation(EnumField.class);
        if (Objects.nonNull(enumField)) {
            return new EnumSerializer(enumField);
        }
        return super.findSerializer(annotated);
    }

    /**
     * 查询反序列化器
     */
    @Override
    public Object findDeserializer(Annotated annotated) {
//        StringToCollection toCollection = annotated.getAnnotation(StringToCollection.class);
//        if (Objects.nonNull(toCollection)) {
        // 这里可以自定义一个反序列化接口 看是要转换成 String[] array ，还是Set<String> 还是 List<String>
//        }
        return super.findDeserializer(annotated);
    }


}
